﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using TodoApp.TMS_personnelmatters.Employeess;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_SetUp.Operators
{
    public class TMS_OperatorRepository : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_OperatorRepository
    {
        public TMS_OperatorRepository(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<ResponseModel<TMS_OperatorDto>> GetLogan(string account,string password)
        {
            ResponseModel<TMS_OperatorDto> response = new ResponseModel<TMS_OperatorDto>();
            var dbConnection = await GetDbConnectionAsync();
            response.Data = (await dbConnection.QueryAsync<TMS_OperatorDto>($"select * from TMS_Operator a join TMS_Employees b on a.Employees_Id=b.Id where b.Employees_Phone = '{account}' and a.Operator_Password = '{password}'", transaction: await GetDbTransactionAsync())).FirstOrDefault();
            if (response.Data != null)
            {
                response.Code = ResponseCode.Success;
            }
            return response;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="tMS_Operator"></param>
        /// <returns></returns>
        public async Task<int> CreateOperator(TMS_Operator tMS_Operator)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"insert into TMS_Operator values(NEWID(),'{tMS_Operator.Operator_SerialNumber}','{tMS_Operator.Operator_CompanyName}','{tMS_Operator.Employees_Id}','{tMS_Operator.Role_Id}','{tMS_Operator.Operator_Password}',0,1,getdate())", null,
                 await GetDbTransactionAsync());
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public async Task<int> DeleteOperator(string id)
        {
            var dbConnection = await GetDbConnectionAsync();
            var count = 0;
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                count += await dbConnection.ExecuteAsync($"update TMS_Operator set Operator_Delete = 'false' where Id = '{item}'", null,
                 await GetDbTransactionAsync());
            }
            return count;
        }
        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="serialNumber"></param>
        /// <param name="name"></param>
        /// <returns></returns>

        public async Task<PageResponse<TMS_OperatorDto>> GetOperator(string serialNumber,string name,int pageindex,int pagesize)
        {
            PageResponse<TMS_OperatorDto> response = new PageResponse<TMS_OperatorDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select a.*,b.Employees_Phone,b.Employees_Name,c.Role_Name from TMS_Operator a join TMS_Employees b on a.Employees_Id=b.Id join TMS_Role c on a.Role_Id = c.Id where Operator_Delete = 'true'";
            var list = await dbConnection.QueryAsync<TMS_OperatorDto>(sql, transaction: await GetDbTransactionAsync());
            response.PageTotal = list.Count();
            if (serialNumber != null)
            {
                sql += $" and a.Operator_SerialNumber = '{serialNumber}'";
            }
            if (name != null)
            {
                sql += $" and b.Employees_Name = '{name}'";
            }
            sql += $" order by a.Operator_DateTime offset {(pageindex-1)*pagesize} rows fetch next {pagesize} rows only";
            var lists = await dbConnection.QueryAsync<TMS_OperatorDto>(sql, transaction: await GetDbTransactionAsync());
            response.PageData = lists.ToList();
            return response;
        }
        /// <summary>
        /// 显示员工
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        
        public async Task<List<TMS_Employees>> GetTMS_Employees()
        {
            var dbConnection = await GetDbConnectionAsync();
            return (await dbConnection.QueryAsync<TMS_Employees>("select * from TMS_Employees where Employees_Delete = 'true'", transaction: await GetDbTransactionAsync())).ToList();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tMS_Operator"></param>
        /// <returns></returns>

        public async Task<int> UpdateOperator(TMS_OperatorDto tMS_Operator)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_Operator set Operator_SerialNumber='{tMS_Operator.Operator_SerialNumber}',Operator_CompanyName='{tMS_Operator.Operator_CompanyName}',Employees_Id='{tMS_Operator.Employees_Id}',Role_Id='{tMS_Operator.Role_Id}',Operator_Password='{tMS_Operator.Operator_Password}' where Id = '{tMS_Operator.Id}'", null,
                 await GetDbTransactionAsync());
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        
        public async Task<ResponseModel<TMS_OperatorDto>> GetFindById(string id)
        {
            ResponseModel<TMS_OperatorDto> response = new ResponseModel<TMS_OperatorDto>();
            var dbConnection = await GetDbConnectionAsync();
            response.Data = (await dbConnection.QueryAsync<TMS_OperatorDto>($"select a.*,b.Id,b.Employees_Phone,b.Employees_Name from TMS_Operator a join TMS_Employees b on a.Employees_Id=b.Id where b.Id = '{id}'", transaction: await GetDbTransactionAsync())).FirstOrDefault();
            return response;
        }

        public async Task<int> UpdatePhone(string name, string id)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_Employees set Employees_Phone = '{name}' where Id = '{id}'", null,
                 await GetDbTransactionAsync());
        }
    }
}
